package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.Parameter;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * 
 * @author Yudi Xue
 * 
 */
public class ParameterID extends BaseId {

	public ParameterID(int value) throws SilverBusinessLogicErrorException {
		super(value);
	}

	// return example: p1, p20
	public String toString() {
		return "p" + idVal;
	}

	public int getIdVal() {
		return idVal;
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((ParameterID) id)
				.getIdVal() == this.getIdVal()));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		String name = borrowedObj.getName(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		return null;
	}

	@Override
	public boolean isEntity() {
		return false;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		ParameterSetID psId = borrowedObj.getParentParameterSetId(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return psId;
	}

	@Override
	public boolean hasChildren() {
		return false;
	}

	public void accept(SilverIDVisitor v) {
		return;
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		return "";
	}

	@Override
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		return false;
	}

	@Override
	public String getTypeFromDatabase(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getType(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public String getDescriptionFromDatabase(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getDescription(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.setDescription(this, desc, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	public String getUnit(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getUnits(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		return null;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		Parameter borrowedObj = ((Parameter) Parameter.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.deleteParameter(this, storage);
		Parameter.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

}
